//
//  UgcMain_First_Tbv_WX.m
//  ZITHER
//
//  Created by weixin on 2019/3/14.
//  Copyright © 2019 weixn. All rights reserved.
//

#import "UgcMain_First_Tbv_WX.h"

#import "UgcMain_FirstOnePic_Cell_WX.h"
#import "UgcMain_FirstFourPic_Cell_WX.h"
#import "UgcMain_FirstMorePic_Cell_WX.h"

#import "Ugc_Detail_VC_WX.h"
@implementation UgcMain_First_Tbv_WX


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        self.delegate = self;
        self.dataSource = self;
        [self Starefreshing];
        self.backgroundColor = HexColor(0xf5f5f5);
        
    }
    return self;
}

-(void)Starefreshing{
    //获取热门个数据
    
    [[serviceDefualt loadGetuserUgcList:@"" isEssence:@"2" page:self.page reponseObjctClass:[UgcMainModel class]]subscribeNext:^(ResponseData * x) {
        if (x.success) {
            UgcMainModel *model = x.responseData;
            NSArray *arr = model.list;
            if (arr.count == 0) {
                if (self.page == 0 ) {
                    [self setNoMoreDataBg:NO NoDataState:NoMoreDataStateDefault];
                    [Tools showTips:@"暂无更多数据"];
                    [self endRefreshing];
                }else{
                    [self.mj_footer endRefreshingWithNoMoreData];
                }
                return ;
                
            }else{
                if (self.page == 0) {
                    self.arrM = [[NSMutableArray alloc]init];
                }
                for (UgcMainModel *model in arr) {
                    [self.arrM addObject: model];
                }
                [self reloadData];
            }
            
        }else{
            [Tools showTips:x.errorInfo];
        }
        [self endRefreshing];
        
    }];
    
}

#pragma mark -- tabdelegate
-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.arrM.count;
}

-(UITableViewCell*)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UgcMainListModel *model = self.arrM[indexPath.row];
    
//    NSLog(@"%@",[model.fileList class]);
        if (model.fileList.count == 1) {
            return [self getFriendsOneCell:indexPath];
        }else if(model.fileList.count == 4){
            return [self getFriendsFourPicCell:indexPath];
        }else{
            return [self getFriendsMorePicCell:indexPath];
        }

}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    UgcMainListModel *model = self.arrM[indexPath.row];
    Ugc_Detail_VC_WX *vc = [[Ugc_Detail_VC_WX alloc]init];
    vc.ugcId = model.ugcId;
    vc.userID = model.userId;
    [vc setBlockDel:^{
        [self.arrM removeObjectAtIndex:self.selectCellIndexPath];
        [self deleteRowsAtIndexPaths:@[indexPath]  withRowAnimation:UITableViewRowAnimationFade];
    }];
    [self.getNav pushViewController:vc animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView estimatedHeightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return UITableViewAutomaticDimension;
}

-(UgcMain_FirstOnePic_Cell_WX*)getFriendsOneCell:(NSIndexPath*)indexPath{
    
    
    
    static NSString *OnePicCell = @"UgcMain_FirstOnePic_Cell_WX";
    UgcMain_FirstOnePic_Cell_WX *cell = [self dequeueReusableCellWithIdentifier:OnePicCell];
    if (cell == nil) {
        cell = [[UgcMain_FirstOnePic_Cell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:OnePicCell];
    }
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    UgcMainListModel *model = self.arrM[indexPath.row];
    cell.model = model;

    
//    static NSString *OnePicCell = @"Ugc_Friends_OnePicCell_WX";
//    Ugc_Friends_OnePicCell_WX *cell = [self dequeueReusableCellWithIdentifier:OnePicCell];
//    if (cell == nil) {
//        cell = [[Ugc_Friends_OnePicCell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:OnePicCell];
//    }
//    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
//    UgcMainListModel *model = self.arrM[indexPath.row];
//    cell.model = model;
    
    
    return cell;
}

-(UgcMain_FirstFourPic_Cell_WX*)getFriendsFourPicCell:(NSIndexPath*)indexPath{
//#import "UgcMain_FirstMorePic_Cell_WX.h"
    static NSString *fourPicCell = @"UgcMain_FirstFourPic_Cell_WX";
    UgcMain_FirstFourPic_Cell_WX *cell = [self dequeueReusableCellWithIdentifier:fourPicCell];
    if (cell == nil) {
        cell = [[UgcMain_FirstFourPic_Cell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:fourPicCell];
    }

    @weakify(cell);
    
    @weakify(self);
    
    [cell setShowBigPic:^(NSIndexPath * _Nonnull indexPath, NSUInteger index) {
        @strongify(cell);
        @strongify(self);
        self.type = NO;
        [self.arrImageView removeAllObjects];
        [self.arrImageUrl removeAllObjects];
        
        self.arrZipImageView = cell.arrImageView;
        
        for (int i = 0; i<cell.model.fileList.count; i++) {
            NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,cell.model.fileList[i]];
            UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
            [imageV sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
            [self.arrImageView addObject:imageV];
            [self.arrImageUrl addObject:str];
        }
        
        [self showBigImage:index];
    }];
    
    cell.indexPath = indexPath;
    
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    
    UgcMainListModel *model = self.arrM[indexPath.row];
    cell.model = model;
    
    return cell;
}

-(UgcMain_FirstMorePic_Cell_WX*)getFriendsMorePicCell:(NSIndexPath*)indexPath{
    static NSString *sectionCell = @"UgcMain_FirstMorePic_Cell_WX";
    //        NSLog(@" ========== %@",sectionCell);
    UgcMain_FirstMorePic_Cell_WX *cell = [self dequeueReusableCellWithIdentifier:sectionCell];
    if (cell == nil) {
        cell = [[UgcMain_FirstMorePic_Cell_WX alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:sectionCell];
    }
    cell.indexPath = indexPath;
    @weakify(cell);
    @weakify(self);
    [cell setShowBigPic:^(NSIndexPath * _Nonnull indexPath, NSUInteger index) {
        @strongify(cell);
        @strongify(self);
                self.type = NO;
                [self.arrZipImageView removeAllObjects];
                [self.arrImageView removeAllObjects];
                [self.arrImageUrl removeAllObjects];
        
                for (int i = 0; i<cell.model.fileList.count; i++) {
                    NSString *str = [NSString stringWithFormat:@"%@%@",RootUrlImage,cell.model.fileList[i]];
                    UIImageView *imageV = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, 50, 50)];
                    [imageV sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"]];
                    [self.arrImageView addObject:imageV];
                    [self.arrImageUrl addObject:str];
                    [self.arrZipImageView addObject:cell.arrImageView[i]];
                }
        
                [self showBigImage:index];
    }];
    cell.selectionStyle = UITableViewCellSeparatorStyleNone;
    UgcMainListModel *model = self.arrM[indexPath.row];
    cell.model = model;
    return cell;
}


-(QMUIImagePreviewViewController *)imagePreviewViewController{
    if (!_imagePreviewViewController) {
        _imagePreviewViewController = [[QMUIImagePreviewViewController alloc] init];
        _imagePreviewViewController.imagePreviewView.delegate = self;
    }
    return _imagePreviewViewController;
}

-(void)showBigImage:(NSInteger)selectIndex{
    UIImageView *imageV = self.arrZipImageView[selectIndex];
    @weakify(self);
    self.imagePreviewViewController.customGestureExitBlock = ^(QMUIImagePreviewViewController *aImagePreviewViewController, QMUIZoomImageView *currentZoomImageView) {
        @strongify(self);
        NSInteger index = aImagePreviewViewController.imagePreviewView.currentImageIndex;
        UIImageView *imageV1 = self.arrZipImageView[index];
        
        UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
        CGRect rect=[imageV1 convertRect: imageV1.bounds toView:window];
        
        [aImagePreviewViewController exitPreviewToRectInScreenCoordinate:rect];
        self.imagePreviewViewController = nil;
    };
    self.imagePreviewViewController.imagePreviewView.currentImageIndex =  selectIndex;// 默认查看的图片的 index
    
    UIWindow * window=[[[UIApplication sharedApplication] delegate] window];
    
    CGRect rect=[imageV convertRect: imageV.bounds toView:window];
    
    [self.imagePreviewViewController startPreviewFromRectInScreenCoordinate:rect cornerRadius:imageV.layer.cornerRadius];
}


#pragma mark - <QMUIImagePreviewViewDelegate>

- (NSUInteger)numberOfImagesInImagePreviewView:(QMUIImagePreviewView *)imagePreviewView {
    return self.arrImageView.count;
}

- (void)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView renderZoomImageView:(QMUIZoomImageView *)zoomImageView atIndex:(NSUInteger)index {
    // 模拟异步加载的情况
    zoomImageView.reusedIdentifier = @(index);
    [zoomImageView showLoading];
    if ([zoomImageView.reusedIdentifier isEqual:@(index)]) {
        //        UIImageView *imageV = ;
        [self.arrImageView[index] sd_setImageWithURL:[NSURL URLWithString:self.arrImageUrl[index]] placeholderImage:[UIImage imageNamed:@"ucg_img_loding"] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
            [zoomImageView hideEmptyView];
            zoomImageView.image = self.arrImageView[index].image;
            
        }];
    }
    
}

- (QMUIImagePreviewMediaType)imagePreviewView:(QMUIImagePreviewView *)imagePreviewView assetTypeAtIndex:(NSUInteger)index {
    return QMUIImagePreviewMediaTypeImage;
}

#pragma mark - <QMUIZoomImageViewDelegate>

- (void)singleTouchInZoomingImageView:(QMUIZoomImageView *)zoomImageView location:(CGPoint)location {
    //点击缩回
    self.imagePreviewViewController.customGestureExitBlock(self.imagePreviewViewController, zoomImageView);
    [self.arrImageView removeAllObjects];
    [self.arrImageUrl removeAllObjects];
    [[SDImageCache sharedImageCache] setValue:nil forKey:@"memCache"];
    
}



- (NSMutableArray<UIImageView *> *)arrImageView{
    if (!_arrImageView) {
        _arrImageView = [[NSMutableArray alloc]init];
    }
    return _arrImageView;
}

-(NSMutableArray *)arrZipImageView{
    if (!_arrZipImageView) {
        _arrZipImageView = [[NSMutableArray alloc]init];
    }
    return _arrZipImageView;
}

-(NSMutableArray *)arrImageUrl{
    if (!_arrImageUrl) {
        _arrImageUrl = [[NSMutableArray alloc]init];
        
    }
    return _arrImageUrl;
}



@end
